package com.wngbms.homework.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TypeHandlerFactory implements BeanFactoryAware, ApplicationContextAware {

    public static final Map<String, Class> typeHandlerMap = new HashMap<>();

    public static BeanFactory beanFactory;

    public static ApplicationContext applicationContext;

    public static  <T> T getHandler(String key) {
        Class<T> aClass = typeHandlerMap.get(key);
        T bean = applicationContext.getBean(aClass);
        return bean;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        TypeHandlerFactory.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        TypeHandlerFactory.applicationContext = applicationContext;
    }
}
